/*
 * 文 件 名 : SysParameterDAO.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-4-26
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.db;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.fire8.dolphin.model.Answer;
import com.fire8.dolphin.model.Question;
import com.fire8.dolphin.utils.DateUtil;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

/**
 * <一句话功能简介><br>
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-4-26]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class QuestionDAO
{
    private final String TAG="dolphin";
    private LixgDBHelper dbHelper;
    public QuestionDAO(Context context)
    {
        dbHelper = new LixgDBHelper(context);
    }
    
    public  void add(Question question)
    {
        //检查是否已存在
        if(isExist(question))
        {
            return;
        }
        ContentValues values = new ContentValues();
        values.put(Question.C_REMOTE_ID, question.getRemoteId());
        values.put(Question.C_TITLE, question.getTitle());
        values.put(Question.C_CONTEXT, question.getContext());
        values.put(Question.C_UPDATE_DATE, question.getUpdateDate());
        values.put(Question.C_ANSWER_NUM, question.getAnswerNum());
        values.put(Question.C_USER_ID, question.getUserId());
        values.put(Question.C_USER_NAME, question.getUserName());
        values.put(Question.C_USER_HEAD, question.getUserHead());
        values.put(Question.C_HEAD_TYPE, question.getHeadType());
        values.put(Question.C_VOICE, question.getVoice());
        values.put(Question.C_PICTURE, question.getPicture());
        values.put(Question.C_VOICE_LONG, question.getVoiceLong());
        values.put(Question.C_SUPPORT_NUM, question.getSupportNum());
        long id = dbHelper.getWritableDatabase().insert(Question.TABLE_NAME, null, values );
        question.setQuesId(String.valueOf(id));
        dbHelper.close();
        
    }
    
    private boolean isExist(Question question)
    {
        boolean isExist=false;
        if(StringUtil.isEmpty(question.getRemoteId()))
        {
            return false;
        }
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Question.C_Q_ID)//id
        .append(" FROM ")
        .append(Question.TABLE_NAME);
        sql.append(" where ").append(Question.C_REMOTE_ID).append("=?");
        args.add(question.getRemoteId());
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        if(cursor != null)
        {
            if(cursor.moveToNext())
            {
                isExist=true;
                LogUtil.d("该记录已存在");
                LogUtil.d("qId:"+question.getRemoteId());
            }
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        return isExist;
    }
    public Question getObject(String id)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Question.C_Q_ID)//id
        .append(",").append(Question.C_REMOTE_ID)//服务器上的ID
        .append(",").append(Question.C_TITLE)
        .append(",").append(Question.C_CONTEXT)
        .append(",").append(Question.C_UPDATE_DATE)
        .append(",").append(Question.C_ANSWER_NUM)
        .append(",").append(Question.C_USER_ID)
        .append(",").append(Question.C_USER_NAME)
        .append(",").append(Question.C_USER_HEAD)
        .append(",").append(Question.C_HEAD_TYPE)
        .append(",").append(Question.C_VOICE)
        .append(",").append(Question.C_PICTURE)
        .append(",").append(Question.C_VOICE_LONG)
        .append(",").append(Question.C_SUPPORT_NUM)
        .append(" FROM ")
        .append(Question.TABLE_NAME)
        .append(" where 1=1 ");
        
        if(id != null)
        {
            sql.append(" and ").append(Question.C_Q_ID).append("=?");
            args.add(id);
        }
        Cursor cursor = dbHelper.getWritableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<Question> list = new ArrayList<Question>();
        cursor2Object(cursor, list);
        dbHelper.close();
        if(!list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }
    
    public List<Question> findList(Question question,Integer operation,Integer pageSize,String orderby)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Question.C_Q_ID)//id
        .append(",").append(Question.C_REMOTE_ID)//服务器上的ID
        .append(",").append(Question.C_TITLE)
        .append(",").append(Question.C_CONTEXT)
        .append(",").append(Question.C_UPDATE_DATE)
        .append(",").append(Question.C_ANSWER_NUM)
        .append(",").append(Question.C_USER_ID)
        .append(",").append(Question.C_USER_NAME)
        .append(",").append(Question.C_USER_HEAD)
        .append(",").append(Question.C_HEAD_TYPE)
        .append(",").append(Question.C_VOICE)
        .append(",").append(Question.C_PICTURE)
        .append(",").append(Question.C_VOICE_LONG)
        .append(",").append(Question.C_SUPPORT_NUM)
        .append(" FROM ")
        .append(Question.TABLE_NAME)
        .append(" where 1=1 ");
        if(question != null)
        {
            if(!StringUtil.isEmpty(question.getUserId()))
            {
                sql.append(" and (").append(Question.C_USER_ID).append("=?").append(" or ").append(Question.C_USER_ID).append(" ='' )");
                args.add(question.getUserId());
            }else
            {
                sql.append(" and ").append(Question.C_REMOTE_ID).append(" is not null ");
            }
            if(!StringUtil.isEmpty(question.getRemoteId()))
            {
                if(Question.QUERY_UP == operation)
                {
                    sql.append(" and ").append(Question.C_REMOTE_ID).append(">?");
                }else
                {
                    sql.append(" and ").append(Question.C_REMOTE_ID).append("<?");
                }
                args.add(question.getRemoteId());
            }
        }
        if("1".equals(orderby))
        {
            sql.append(" order by ").append(Question.C_REMOTE_ID).append(" desc ");
        }else if("2".equals(orderby))
        {
            sql.append(" order by ").append(Question.C_ANSWER_NUM);
        }else if("3".equals(orderby))
        {
            sql.append(" order by ").append(Question.C_ANSWER_NUM).append(" desc ");
        }
        sql.append(" limit 0,").append(pageSize);
        Log.i(TAG,"问题查询SQL："+sql.toString());
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<Question> list = new ArrayList<Question>();
        cursor2Object(cursor, list);
        
        dbHelper.close();
        return list;
    }

    /**
     * <一句话功能简介>
     * <功能详细描述>
     * @param cursor
     * @param list [参数说明]
     *
     * @author lixg
     * @see [类、类#方法、类#成员]
     */
    private void cursor2Object(Cursor cursor, List<Question> list)
    {
        if(cursor != null)
        {
            while(cursor.moveToNext())
            {
                
                Question q = new Question();
                q.setQuesId(cursor.getString(cursor.getColumnIndex(Question.C_Q_ID)));
                q.setRemoteId(cursor.getString(cursor.getColumnIndex(Question.C_REMOTE_ID)));
                q.setTitle(cursor.getString(cursor.getColumnIndex(Question.C_TITLE)));
                q.setContext(cursor.getString(cursor.getColumnIndex(Question.C_CONTEXT)));
                q.setUpdateDate(cursor.getString(cursor.getColumnIndex(Question.C_UPDATE_DATE)));
                q.setAnswerNum(cursor.getString(cursor.getColumnIndex(Question.C_ANSWER_NUM)));
                q.setUserId(cursor.getString(cursor.getColumnIndex(Question.C_USER_ID)));
                q.setUserName(cursor.getString(cursor.getColumnIndex(Question.C_USER_NAME)));
                q.setUserHead(cursor.getString(cursor.getColumnIndex(Question.C_USER_HEAD)));
                q.setHeadType(cursor.getString(cursor.getColumnIndex(Question.C_HEAD_TYPE)));
                q.setVoice(cursor.getString(cursor.getColumnIndex(Question.C_VOICE)));
                q.setPicture(cursor.getString(cursor.getColumnIndex(Question.C_PICTURE)));
                q.setVoiceLong(cursor.getLong(cursor.getColumnIndex(Question.C_VOICE_LONG)));
                q.setSupportNum(cursor.getInt(cursor.getColumnIndex(Question.C_SUPPORT_NUM)));
                list.add(q);
            }
            cursor.close();
            cursor = null;
        }
    }
    
    public void update(Question question)
    {
        ContentValues values = new ContentValues();
        values.put(Question.C_REMOTE_ID, question.getRemoteId());
        values.put(Question.C_USER_ID, question.getUserId());
        values.put(Question.C_ANSWER_NUM, question.getAnswerNum());
        String[] args = {question.getQuesId()};
        dbHelper.getWritableDatabase().update(Question.TABLE_NAME, values, Question.C_Q_ID +" =?", args);
        dbHelper.close();
    }
    
    public void delete(String id)
    {
        String[] args = {id};
        dbHelper.getWritableDatabase().delete(Answer.TABLE_NAME, Answer.Q_ID +" =?", args);
        dbHelper.getWritableDatabase().delete(Question.TABLE_NAME, Question.C_Q_ID +" =?", args);
    }
    
}
